home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 25 / AMIGAplus Sonderheft 25 (2000)(Falke)(DE)(Track 1 of 4)[!].iso / PublicDomain / Anwendungen / Lunar / Original / lunar.c < prev    next >
C/C++ Source or Header  |  1991-06-21  |  23KB  |  838 lines

  1. char version[] = "Lunar Version 2.0 (June 20, 1991)";
  2.  
  3. /*----------------------------------------------------------------------------
  4.  
  5.  
  6.           Lunar 2.0: A Calendar Conversion Program
  7.                 for
  8.     Gregorian Solar Calendar and Chinese Lunar Calendar
  9.     ---------------------------------------------------
  10.  
  11.     By Fung F. Lee and Ricky Yeung
  12.  
  13.     Copyright (C) 1988,1989,1991  Fung F. Lee and Ricky Yeung
  14.  
  15.     All rights reserved.
  16.     Permission to copy and distribute verbatim copies of this
  17.     document for non-commercial purposes is granted, but changing
  18.     it is not allowed.
  19.     There is absolutely no warranty for this program.
  20.  
  21.     Please send your comments and suggestions to the authors:
  22.     Fung F. Lee    lee@milo.stanford.edu
  23.     Ricky Yeung    yeung@decwrl.dec.com or yeung@atd.dec.com
  24.     
  25.     The special "bitmap" file "lunar.bitmap" was contributed
  26.     by Weimin Liu (weimin@alpha.ece.jhu.edu).
  27.  
  28. ----------------------------------------------------------------------------*/
  29.  
  30. /*
  31. This document contains Highest-bit-set GuoBiao (HGB) code, as adopted by
  32. CCDOS on IBM PC compatibles, ChineseTalk 6.0 (GB version) on Macintosh,
  33. and cxterm on UNIX and X window.  Hence, one may need to transfer this 
  34. document as a **binary** file.
  35.  
  36. References:
  37. 1. "Zhong1guo2 yin1yang2 ri4yue4 dui4zhao4 wan4nian2li4" by Lin2 Qi3yuan2.
  38.    ¡¶ÖйúÒõÑôÈÕÔ¶ÔÕÕÍòÄêÀú¡·£®ÁÖ
  39. 2. "Ming4li3 ge2xin1 zi3ping2 cui4yan2" by Xu2 Le4wu2.
  40.    ¡¶ÃüÀí¸ïÐÂ×Óƽ´âÑÔ¡·£®Ðì
  41.  
  42. */ 
  43.  
  44.  
  45. #include <stdio.h>
  46. #include <string.h>
  47.  
  48. /* "Bitmap" constants */
  49. #define    BMRow    7    /* number of rows for each bitmap */
  50. #define    BMCol    11    /* number of columns for each bitmap */
  51. #define    NBM    26    /* number of bitmaps */
  52. #define NianBM  0
  53. #define YueBM   1
  54. #define RiBM    2
  55. #define ShiBM   3
  56. #define    GanBM    4
  57. #define    ZhiBM    14
  58.  
  59. #define Cyear    1900    /* Note that LC1900.1.1 is SC1900.1.31 */
  60. #define Nyear    101    /* number of years covered by the table */
  61. #define Nmonth  13    /* maximum number of months in a lunar year */
  62.  
  63. typedef struct {
  64. int year, month, day, hour, weekday;
  65. int leap;    /* the lunar month is a leap month */
  66. } Date;
  67.  
  68. static Date SolarFirstDate = {
  69.     /* Wednesday, 12 a.m., 31 January, 1900 */
  70.     1900, 1, 31, 0, 3, 0
  71.     };
  72.  
  73. static Date LunarFirstDate = {
  74.     /* Wednesday, 12 a.m., First day, First month, 1900 */
  75.     1900, 1, 1, 0, 3, 0
  76.     };
  77.  
  78. static Date GanFirstDate = {
  79.     /* geng1-nian2 wu4-yue4 jia3-ri4 jia3-shi2 */
  80.        6,          4,       0,       0,           3, 0
  81.     };
  82.  
  83. static Date ZhiFirstDate = {
  84.     /* zi3-nian2 yin2-yue4 chen2-ri4 zi3-shi2 */
  85.        0,        2,        4,        0,           3, 0
  86.     };
  87.  
  88. static int daysInSolarMonth[13] = {
  89.     0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  90.  
  91. static int moon[2] = {29,30}; /* a short (long) lunar month has 29 (30) days */
  92.  
  93. static long yearInfo[Nyear] = {
  94.     /* encoding:
  95.         b bbbbbbbbbbbb bbbb
  96.        bit#        1 111111000000 0000
  97.             6 543210987654 3210
  98.             . ............ ....
  99.        month#      000000000111
  100.             M 123456789012   L
  101.                 
  102.     b_j = 1 for long month, b_j = 0 for short month
  103.     L is the leap month of the year if 1<=L<=12; NO leap month if L = 0.
  104.     The leap month (if exists) is long one iff M = 1.
  105.     */
  106.                         0x04bd8,    /* 1900 */
  107.     0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950,    /* 1905 */
  108.     0x16554, 0x056a0, 0x09ad0, 0x055d2, 0x04ae0,    /* 1910 */
  109.     0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540,    /* 1915 */
  110.     0x0d6a0, 0x0ada2, 0x095b0, 0x14977, 0x04970,    /* 1920 */
  111.     0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54,    /* 1925 */
  112.     0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566,    /* 1930 */
  113.     0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60,    /* 1935 */
  114.     0x186e3, 0x092e0, 0x1c8d7, 0x0c950, 0x0d4a0,    /* 1940 */
  115.     0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0,    /* 1945 */
  116.     0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0,    /* 1950 */
  117.     0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573,    /* 1955 */
  118.     0x052d0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6,    /* 1960 */
  119.     0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260,    /* 1965 */
  120.     0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0,    /* 1970 */
  121.     0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250,    /* 1975 */
  122.     0x0d558, 0x0b540, 0x0b5a0, 0x195a6, 0x095b0,    /* 1980 */
  123.     0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50,    /* 1985 */
  124.     0x06d40, 0x0af46, 0x0ab60, 0x09570, 0x04af5,    /* 1990 */
  125.     0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58,    /* 1995 */
  126.     0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960        /* 2000 */
  127. };
  128.  
  129. /*
  130.   In "4-column" calculation, a "mingli" (fortune-telling) calculation,
  131.   the beginning of a month is not the first day of the month as in
  132.   the Lunar Calendar; it is instead governed by "jie2" (festival).
  133.   Interestingly, in the Solar calendar, a jie always comes within three
  134.   days after some fixed date. For example, the jie "li4chun1" (beginning
  135.   of spring) always comes within three days after Feburary 4 of the Solar
  136.   Calendar. This is true at least for the years between 1900 and 2000.
  137.   The table may need to be adjusted if exceptions are found when
  138.   the range is extended.
  139.  
  140.   Meaning of array jie:
  141.   For example, the first integer 4 means the beginning of the 1st month
  142.   is no earlier than Feburary 4; the 12th integer 5 means the beginning of
  143.   the 12th month is no earlier than January 5 (of the next Solar year) in
  144.   the Solar Calendar.
  145.  
  146.   Meaning of array fest:
  147.   Each 24 bit number represents the offsets of the 12 jie's of the year
  148.   from the reference days in jie[]. For example, the number corresponding to
  149.   the "Lunar Year" 1991 is 0x154551, and the two least significant bits
  150.   (bit-1, bit-0) are 01. This means that the first day of the 12th month of
  151.   the "Lunar Year" 1991 falls on January 6, 1992, one day after the reference
  152.   date January 5 as in jie[]. (Incidentally, the first day of that lunar month
  153.   is on January 5.)
  154. */
  155.  
  156. short jie[] = {4, 5, 4, 5, 5, 7, 7, 7, 8, 7, 7, 5};
  157.  
  158. long fest[Nyear] = {
  159.                         0x154551,     /* 1900 */
  160. 0x155555, 0x599555, 0x6a9a56, 0x554551, 0x155555,     /* 1905 */
  161. 0x595555, 0x6a9a56, 0x554551, 0x155555, 0x595555,     /* 1910 */
  162. 0x6a9a56, 0x554551, 0x155555, 0x155555, 0x599655,     /* 1915 */
  163. 0x554511, 0x155551, 0x155555, 0x599655, 0x554511,     /* 1920 */
  164. 0x155561, 0x155555, 0x599655, 0x554511, 0x155551,     /* 1925 */
  165. 0x155555, 0x599555, 0x554501, 0x154551, 0x155555,     /* 1930 */
  166. 0x599555, 0x554501, 0x154551, 0x155555, 0x595555,     /* 1935 */
  167. 0x554501, 0x154551, 0x155555, 0x595555, 0x554501,     /* 1940 */
  168. 0x154551, 0x155555, 0x595555, 0x544501, 0x154511,     /* 1945 */
  169. 0x155555, 0x155555, 0x444100, 0x154511, 0x155555,     /* 1950 */
  170. 0x155555, 0x444100, 0x154511, 0x155551, 0x155555,     /* 1955 */
  171. 0x444100, 0x154511, 0x154551, 0x155555, 0x444000,     /* 1960 */
  172. 0x154501, 0x154551, 0x155555, 0x444000, 0x154501,     /* 1965 */
  173. 0x154551, 0x155555, 0x440000, 0x154501, 0x154551,     /* 1970 */
  174. 0x155555, 0x440000, 0x144501, 0x154551, 0x155555,     /* 1975 */
  175. 0x400000, 0x144101, 0x154511, 0x155555, 0x400000,     /* 1980 */
  176. 0x144101, 0x154511, 0x155555, 0x0,    0x44100,     /* 1985 */
  177. 0x154511, 0x154551, 0x0,      0x44000,  0x154511,     /* 1990 */
  178. 0x154551, 0x0,        0x44000,  0x154501, 0x154551,     /* 1995 */
  179. 0x0,      0x40000,  0x154501, 0x154551, 0x0        /* 2000 */
  180. };
  181.  
  182. static    char    *Gan[] = {
  183.     "Jia3",    "Yi3",     "Bing3", "Ding1", "Wu4",
  184.     "Ji3",    "Geng1", "Xin1",  "Ren2",  "Gui3"
  185. };
  186.  
  187. static    char    *Zhi[] = {
  188.     "Zi3",    "Chou3", "Yin2",  "Mao3",  "Chen2", "Si4",
  189.     "Wu3",    "Wei4",     "Shen1", "You3",  "Xu1",   "Hai4"
  190. };
  191.  
  192. static    char   *ShengXiao[] = {
  193.     "Mouse", "Ox", "Tiger", "Rabbit", "Dragon", "Snake",
  194.     "Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"
  195. };
  196.  
  197. static char *weekday[] = {
  198.     "Sunday", "Monday", "Tuesday", "Wednesday",
  199.     "Thursday", "Friday", "Saturday"
  200. };
  201.  
  202. static    char    *GanGB[] = {
  203.     "¼×", "ÒÒ", "±û", "¶¡", "Îì",
  204.     "¼º", "¸ý", "ÐÁ", "ÈÉ", "¹ï"
  205. };
  206.  
  207. static    char    *ZhiGB[] = {
  208.     "×Ó", "³ó", "Òú", "î", "³½", "ËÈ",
  209.     "Îç", "δ", "Éê", "ÓÏ", "Ðç", "º¥"
  210. };
  211.  
  212. static    char   *ShengXiaoGB[] = {
  213.     "Êó", "Å£", "»¢", "ÍÃ", "Áú", "Éß",
  214.     "Âí", "Ñò", "ºï", "¼¦", "¹·", "Öí"
  215. };
  216.  
  217. static char *weekdayGB[] = {
  218.     "ÈÕ", "Ò»", "¶þ", "Èý",
  219.     "ËÄ", "Îå", "Áù"
  220. };
  221.  
  222.  
  223. Date solar, lunar, gan, zhi, gan2, zhi2, lunar2;
  224.  
  225. int ymonth[Nyear];    /* number of lunar months in the years */
  226. int yday[Nyear];    /* number of lunar days in the years */
  227. int mday[Nmonth+1];    /* number of days in the months of the lunar year